package com.xiaohudie.controller;

import cn.hutool.core.bean.BeanUtil;
import com.xiaohudie.params.LoginParams;
import com.xiaohudie.pojo.AjaxResult;
import com.xiaohudie.pojo.MyThreadLocal;
import com.xiaohudie.pojo.User;
import com.xiaohudie.servier.Impi.UserServiceImpl;
import com.xiaohudie.servier.PhoneServicer;
import com.xiaohudie.util.IsPhone;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author xiao hu die
 * @Date 2022/3/2 15:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/phone")
public class PhoneController {
    @Resource
    private PhoneServicer phoneServicer;
    /**注入redis工具*/
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserServiceImpl userService;
    @GetMapping("/code")
    public String code(@RequestParam("phone") String phone,
                           HttpSession session){
        String code = phoneServicer.sendPhone(phone, session);
      /*  String code = (String) session.getAttribute("code");
        User user = new User();
        user.setPhone(phone);
        user.setName("李家龙");
        session.setAttribute("user",user);*/

        System.out.println(code);
        return code;
    }
    @GetMapping("/test")
    public String test(){
        return "aaaaaaaaaaaa";
    }
    @PostMapping("/login")
    public AjaxResult login(@RequestBody() LoginParams loginParams,
                            HttpSession session){
        /**校验手机号*/
        boolean b = IsPhone.checkMobile(loginParams.getPhone());
        System.out.println(b);
        /**如果成功就是校验验证码放行*/
        if (b){
         //   String code = (String) session.getAttribute("code");
            String code = stringRedisTemplate.opsForValue().get("login:code:" + loginParams.getPhone());
            System.out.println(code);
            Optional<String> code1 = Optional.ofNullable(code);
            /**登录成功之后需要将用户信息放入redis中
             * 使用hash结构*/
            /**生成token*/
            String token = UUID.randomUUID().toString();
            /**根据手机号查询这个用户*/
            User user = userService.getUser(loginParams.getPhone());
            if (user==null){
                return AjaxResult.error("该用户不存在，请先去注册");
            }
            // User user = new User("18156081706", "刘碟");
            Map<String, Object> userMap = BeanUtil.beanToMap(user);
            /**存放入redis中*/
            stringRedisTemplate.opsForHash().putAll(token,userMap);
            /**设置过期时间*/
            stringRedisTemplate.expire(token,200000, TimeUnit.MILLISECONDS);
            /**得到ThreadLocal*/
            if (code1.isPresent() && code.equals(loginParams.getCode())){
               /* User user = MyThreadLocal.getUser();
                System.out.println(user);*/
                return AjaxResult.success("登录成功");
            }
        }
        return AjaxResult.error("手机号格式不正确或者验证码错误");
    }
    @GetMapping("/user")
    public AjaxResult getUser(){
        User user = MyThreadLocal.getUser();
        return AjaxResult.success(user);
    }
    @GetMapping("/frontPage")
   public AjaxResult frontPage(){
        return AjaxResult.success("这就是首页，你进入成功了");
    }
    @GetMapping("/testThread")
    public AjaxResult testThread(){
        User user = MyThreadLocal.getUser();
        return AjaxResult.success(user);
    }
}
